package com.example.yang.yixin.FragmentManager;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.yang.yixin.Adapter.PersonAdapter;
import com.example.yang.yixin.Adapter.SignAdapter;
import com.example.yang.yixin.R;
import com.example.yang.yixin.Utils.Person_info;
import com.example.yang.yixin.Utils.Sign_info;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class Fragment_All extends Fragment {
    View view;
    private List<Sign_info> Sign_info=new ArrayList<>();
    private List<String> list=new ArrayList<>();
    private RecyclerView mRecyclerView;
    private SignAdapter madpater;
     SmartRefreshLayout smartRefreshLayout;
    String bkpassword;
    Context context;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view =inflater.inflate(R.layout.fragment_all,container,false);
        mRecyclerView=view.findViewById(R.id.recyclerView2);
        LinearLayoutManager layoutManager=new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(view.getContext(),DividerItemDecoration.VERTICAL));
        smartRefreshLayout=view.findViewById(R.id.smart_refresh2);
        bkpassword=getArguments().getString("bkpassword");
        getServerdata();
        return view;
    }
    private void getServerdata() {
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/SigninfoServlet?bkpassword="+bkpassword;
        RequestQueue mQueue = Volley.newRequestQueue(view.getContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {

                    String username;
                    String time;
                    String name;
                    String Mosttime;
                    int size=Integer.parseInt(response.getString("size")) ;
                    for (int i=0;i<size;i++) {
                        /*
                        这个地方要修改
                         */
                        username = new String(response.getString("username"+i));
                        name = new String(response.getString("name"+i));
                        time = new String(response.getString("time"+i));
                        Mosttime=new String(response.getString("Mosttime"));

                        Sign_info a = new Sign_info(username, name,time,Mosttime);
                        Sign_info.add(a);
                    }
                    madpater=new SignAdapter(Sign_info);

                    mRecyclerView.setAdapter(madpater);

                } catch (JSONException e) {

                    Log.e("json错误",e.getMessage());
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                }catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);

    }
}
